//dd爱框框
import java.util.*;
import java.io.*;

public class Main{
    private static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    
    public static void main(String[] args) throws IOException {
        Read in = new Read();
        int n = in.nextInt();
        int x = in.nextInt();
        int[] arr = new int[n+1];
        for (int i = 1; i <= n; i++) {
            arr[i] = in.nextInt();
        }
        int sum = 0;
        int l = 1;
        int r = 1;
        int[] ret = new int[2];
        while (r < n+1) {
            sum += arr[r];
            if (sum >= x) {
                while (r >= l && sum >= x) {
                    sum -= arr[l++];
                }
                if (ret[0] == 0 || ret[1] - ret[0] > r-l+1) {
                    ret[0] = l-1;
                    ret[1] = r;
                }
            }
            r++;
        }
        out.println(ret[0]+" "+ret[1]);
        
        out.close();
    }
}

class Read{
    private StringTokenizer st = new StringTokenizer("");
    private BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

    public String next() throws IOException {
        while(!st.hasMoreTokens()) {
            st = new StringTokenizer(bf.readLine());
        }
        return st.nextToken();
    }
    
    public int nextInt() throws IOException{
        return Integer.parseInt(next());
    }
}
“小红的树”改良版：

#include <iostream>
using namespace std;

struct ChildNode
{
    int child;
    struct ChildNode *next;
};

struct ParentNode
{
    int i;  
    struct ChildNode *next;
};

int Count(char *color, struct ParentNode *arr, int i)
{
    int count = 0;
    if (color[i] == 'R')
    {

        count++;
        
    }
    struct ChildNode *p = arr[i].next;
    while(p != NULL)
    {
        count += Count(color, arr, p->child);
        p = p->next;
    }
    
    return count;
}


int main() 
{

    int n = 0;
    cin >> n;
    int a = 0;
    int q = 0;
    struct ParentNode arr[n];
    struct ChildNode *z;
    for (int i = 0; i < n; i++)
    {
        arr[i].i = 0;
        arr[i].next = NULL;
    }
    for (int i = 1; i < n; i++)
    {
        cin >> a;
        struct ChildNode *p = new struct ChildNode;
        p->child = i;
        p->next = NULL;
        z = arr[a-1].next;
        if (z == NULL)
        {
            arr[a-1].next = p; 
        }
        else 
        {
            while(z->next != NULL)
            {
                z = z->next;
            }
            z->next = p;
        }
    }
    char color[n+1];
    cin >> color;

    
    for (int j = 0; j < n; j++){
        arr[j].i = Count(color, arr, j);
       // cout << arr[j].i<< endl;
    }


    cin >> q;
    int x = 0;
    while(q--)
    {
        cin >> x;
        cout << arr[x-1].i << endl;
    }

    return 0;
}
//153
class Solution {
    public int findMin(int[] nums) {
        int len = nums.length;
        if (nums[0] < nums[len-1] || len == 1) {
            return nums[0];
        }
        
        int l = 0;
        int r = len-1;
        while (l <= r) {
            int mid = (l+r) / 2;
            if (mid-1 >= 0 && nums[mid] < nums[mid-1]) {
                return nums[mid];
            } else if (nums[mid] >= nums[len-1]){
                l = mid+1;
            } else {
                r = mid-1;
            }
        }
        return nums[l];
    }
}